package leetcode;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

public class LeetCodeMain115 {
    public int nthUglyNumber(int n) {
        PriorityQueue<Long> queue=new PriorityQueue<>();
        queue.offer(1L);
        int[] factors={2,3,5};
        Set<Long> set=new HashSet<>();
        int count=0;
        for (int i = 0; i < n; i++) {
            long num=queue.poll();
            count=(int)num;
            for (int number:factors) {
                long next=num*number;
                if (set.add(next)){
                    queue.offer(next);
                }
            }
        }
        return count;
    }

}
